package leetcode;

import java.util.ArrayList;
import java.util.List;

class Solution_93 {
    private List<String> result = new ArrayList<>();

    public List<String> restoreIpAddresses(String s) {
        if (s.length() < 4 || s.length() > 12) return result;
        backtracking(s,s.length(),0,"");
        return result;
    }

    public void backtracking(String str,int len, int start, String temp) {
        if (temp.split("\\.").length > 4) return;
        if (temp.length() == len + 4){
            result.add(temp.substring(0,len + 3));
        }
        for (int i = start; i < start + 3 && i < len; i++) {
            int num = Integer.parseInt(str.substring(start,i + 1));
            if (num > 255) break;
            backtracking(str,len,i + 1,temp + num + '.');
            if (num == 0 && i == start) break;
        }
    }

    public static void main(String[] args) {
        String test1 = "101023";
        List<String> strings = new Solution_93().restoreIpAddresses(test1);
        System.out.println("---------------------");
        for (String string : strings) {
            System.out.println(string);
        }
        System.out.println("---------------------");

    }
}